package com.levi.utils.image;

import android.content.Context;
import android.graphics.Bitmap;

import java.io.File;
import java.io.IOException;

/**
 * Created by Levi on 2018/3/25.
 */

public class Compressor {
    //max width and height values of the compressed image is taken as 612x816
    private int mMaxWidth = 800;
    private int mMaxHeight = 800;
    private Bitmap.CompressFormat mCompressFormat = Bitmap.CompressFormat.JPEG;
    private int mQuality = 80;

    public Compressor(Context context) {
    }

    public Compressor setMaxWidth(int maxWidth) {
        this.mMaxWidth = maxWidth;
        return this;
    }

    public Compressor setMaxHeight(int maxHeight) {
        this.mMaxHeight = maxHeight;
        return this;
    }

    public Compressor setCompressFormat(Bitmap.CompressFormat compressFormat) {
        this.mCompressFormat = compressFormat;
        return this;
    }

    public Compressor setQuality(int quality) {
        this.mQuality = quality;
        return this;
    }

    public File compressToFile(File imageFile, File resultFile) throws IOException {
        return ImageUtil.compressImage(imageFile,
                mMaxWidth,
                mMaxHeight,
                mCompressFormat,
                mQuality,
                resultFile.getAbsolutePath());
    }

    public Bitmap compressToBitmap(File imageFile) throws IOException {
        return ImageUtil.decodeSampledBitmapFromFile(imageFile, mMaxWidth, mMaxHeight);
    }

}
